package com.zaimokuza.cn.config;

import com.zaimokuza.cn.config.interceptor.ResponseInterceptor;
import com.zaimokuza.cn.entity.constants.FileConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvcConfigurer实现配置类
 *
 * @author zaimokuza
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public ResponseInterceptor responseInterceptor() {
        return new ResponseInterceptor();
    }

    /**
     * 配置拦截器，默认加入的顺序就是拦截器的执行顺序
     * 具体的方法执行顺序：1-N的preHandle，N-1的postHandle，N-1的afterCompletion
     * 也可以通过.order()方法来进行指定拦截器执行顺序
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 自定义后方路径,常见的比如"/**","/api/**"
        registry.addInterceptor(responseInterceptor()).order(1).addPathPatterns("/**");
    }

    /**
     * 配置虚拟路径
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(FileConstant.IMAGE_VIRTUAL_FOLDER + "**").addResourceLocations("file:" + FileConstant.IMAGE_ACTUAL_FOLDER);
    }

}
